﻿@{
    Layout = null;
    var tableName = PageViewModel.PageTableID(this);
    var tableToolBar = PageViewModel.PageTableToolbar(this);

    var editPositionDlgName = PageViewModel.PageEditDialog(this);
    var editPositionFunctionName = PageViewModel.PageEditDialog(this, "edit_position", "js_fun");
    var savePositionFunctionName = PageViewModel.PageFunctionName(this, "save_edit_position");
    var deletePositionFunctionName = PageViewModel.PageFunctionName(this, "delete_position");
}
<div style="height:100%">

    <script type="text/javascript">
    function @editPositionFunctionName (id) {
        var dlg = $dlg('@editPositionDlgName');
        dlg.dialog({
            title: '岗位信息', width: 600, height: 450,
            closed: false, cache: false, modal: true, maximizable: false, resizable: true,
            href: '~/Positions/_EditPosition/' + id,
            onResize: function () {
                $(this).dialog('center');
            },
            buttons: [
                {
                    text: "保 存",
                    handler: function () { @savePositionFunctionName (); },
                    iconCls: "icon-save"
                }, {
                    text: "取 消",
                    handler: function () { $("#@editPositionDlgName").dialog("close"); },
                    iconCls: "icon-cancel"
                }
            ],
            onLoad: function () {
                LoadObject(id, "~/Positions/GetPositionByID/", function (position) {
                    var form = $("#@editPositionDlgName").find("form");
                    form.form("load", position);
                    if (position.Enable) {
                        form.find("#Enable").switchbutton("check");
                    } else {
                        form.find("#Enable").switchbutton("uncheck");
                    }
                    if (position.Critical) {
                        form.find("#Critical").checkbox("check");
                    } else {
                        form.find("#Critical").checkbox("uncheck");
                    }
                });
            }
        });
    }

    function @savePositionFunctionName () {
        var form = $("#@editPositionDlgName").find("form");
        if (form.form('enableValidation').form('validate')) {
            var position = form.form_to_json();
            var node = form.find("#DepartmentID").combotree("tree").tree("getSelected");
            if (node != null) {
                position.OrgID = node.OrgID;
                if (node.NodeType == "ORG") {
                    position.DepartmentID = 0;
                }
            }
            position.Enable = form.find("#Enable").switchbutton("options").checked;
            position.Critical = form.find("#Critical").checkbox("options").checked;

            $.ajax({
                url: "~/Positions/SavePosition",
                type: "POST",
                data: position,
                success: function (e) {
                    if (e.Result) {
                        $.messager.alert('保存成功', '保存成功!', 'info', function () {
                            $('#@editPositionDlgName').dialog('close');
                            $("#@tableName").treegrid("reload");
                        });
                    } else {
                        $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e) {
                    ShowAjaxError(e);
                }
            });
        }
    }

    function @deletePositionFunctionName (id) {
        if (id > 0 && confirm("确认删除岗位信息?")) {
            $.ajax({
                async: false,
                method: "DELETE",
                url: $.Content("~/Positions/DeletePosition/" + id),
                context: $("#@tableName"),
                success: function (e) {
                    if (e.Result) {
                        this.treegrid('reload');
                    } else {
                        $.messager.alert('删除数据错误', '删除失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e1, e2, e3) {
                    ShowAjaxError(e1, "删除数据错误");
                    this.treegrid('reload');
                }
            });
        }
    }
    </script>

    <table id="@tableName" class="easyui-treegrid" style="width:100%;height:100%" rownumbers="true" fit="true" fitcolumns="true"
           data-options="striped:true,border:false,singleSelect:true,url:'~/Positions/GetPositionTrees',method:'get',idField: 'Id',treeField: 'OrgID',toolbar:'#@tableToolBar'">
        <thead>
            <tr>
                <th data-options="field:'_edit',align:'center',formatter:function(v,r,i){
                    if(r.NodeType == 'POS'){
                        return GridRowEditFmt('@editPositionFunctionName', r.Id);
                    }
                }" width="45">修改</th>
                <th data-options="field:'_remove',align:'center',formatter:function(v,r,i){
                    if(r.NodeType == 'POS'){
                        return GridRowDeleteFmt('@deletePositionFunctionName', r.Id);
                    }
                }" width="45">删除</th>
                <th data-options="field:'OrgID',width:140,formatter:function(v,r,i){
                    if(r.NodeType == 'POS'){
                        var u = GetObject(r.DepartmentID, '~/Departments/GetDepartmentByID');
                        if(u != null) {
                            return u.FullName;
                        } else {
                            u = GetObject(r.OrgID, '~/Organization/GetOrganizationByID');
                            if(u != null) { return u.FullName; }
                        }
                    }else{
                        return '&lt;div style=\'color:#cdcdcd\'&gt;'+r.TempDepartmentName+'&lt;/div&gt;';
                    }
                }">所属部门</th>
                <th data-options="field:'Code',width:80">岗位编号</th>
                <th data-options="field:'Name',width:160">岗位名称</th>
                <th data-options="field:'Critical',width:80,align:'center',formatter:$.CheckdFormat">关键岗位</th>
                <th data-options="field:'Enable',width:80,align:'center',formatter:$.CheckdFormat">启用</th>
                <th data-options="field:'_',width:140">岗位类别</th>
            </tr>
        </thead>
    </table>
    <div id="@tableToolBar" class="table-toolbar">
        <a href="javascript:@editPositionFunctionName ()" class="easyui-linkbutton t-btn"><i class="fa fa-plus-circle icon" style="color:green"></i><span>新增</span></a>
    </div>
</div>